
#include <goto_line_panel.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>




struct _GoToLinePanelPrivate {
	GtkSpinButton* line_number;
	GtkLabel* line_count_lbl;
};

#define GO_TO_LINE_PANEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_GO_TO_LINE_PANEL, GoToLinePanelPrivate))
enum  {
	GO_TO_LINE_PANEL_DUMMY_PROPERTY
};
static gboolean _go_to_line_panel_on_goto_line_gtk_widget_key_press_event (GtkSpinButton* _sender, const GdkEventKey* event, gpointer self);
static gboolean _go_to_line_panel_on_show_panel_doc_panel_base_show_panel (GoToLinePanel* _sender, gpointer self);
static gboolean go_to_line_panel_real_on_show_panel (GoToLinePanel* self);
static gpointer go_to_line_panel_parent_class = NULL;
static void go_to_line_panel_finalize (GObject* obj);
static int _vala_strcmp0 (const char * str1, const char * str2);



static gboolean _go_to_line_panel_on_goto_line_gtk_widget_key_press_event (GtkSpinButton* _sender, const GdkEventKey* event, gpointer self) {
	return go_to_line_panel_on_goto_line (self, _sender, event);
}


static gboolean _go_to_line_panel_on_show_panel_doc_panel_base_show_panel (GoToLinePanel* _sender, gpointer self) {
	return go_to_line_panel_on_show_panel (self);
}


GoToLinePanel* go_to_line_panel_construct (GType object_type, GtkSourceBuffer** source_buff, GtkSourceView** source_view) {
	GoToLinePanel * self;
	GtkImage* _tmp0;
	GtkLabel* desc;
	GtkSpinButton* _tmp1;
	GtkLabel* _tmp2;
	g_return_val_if_fail (source_buff != NULL, NULL);
	g_return_val_if_fail (source_view != NULL, NULL);
	self = (GoToLinePanel*) doc_panel_base_construct (object_type, (guint) 1, (guint) 5, FALSE, &(*source_buff), &(*source_view));
	_tmp0 = NULL;
	gtk_table_attach ((GtkTable*) self, (GtkWidget*) (_tmp0 = g_object_ref_sink ((GtkImage*) gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU))), (guint) 0, (guint) 1, (guint) 0, (guint) 1, GTK_SHRINK, GTK_SHRINK, (guint) 5, (guint) 0);
	(_tmp0 == NULL) ? NULL : (_tmp0 = (g_object_unref (_tmp0), NULL));
	desc = g_object_ref_sink ((GtkLabel*) gtk_label_new ("<b>Przejdź do lini: </b>"));
	gtk_label_set_use_markup (desc, TRUE);
	gtk_table_attach ((GtkTable*) self, (GtkWidget*) desc, (guint) 1, (guint) 2, (guint) 0, (guint) 1, GTK_SHRINK, GTK_SHRINK, (guint) 5, (guint) 0);
	_tmp1 = NULL;
	self->priv->line_number = (_tmp1 = g_object_ref_sink ((GtkSpinButton*) gtk_spin_button_new_with_range ((double) 1, (double) 1, (double) 1)), (self->priv->line_number == NULL) ? NULL : (self->priv->line_number = (g_object_unref (self->priv->line_number), NULL)), _tmp1);
	gtk_widget_set_size_request ((GtkWidget*) self->priv->line_number, 150, -1);
	g_signal_connect_object ((GtkWidget*) self->priv->line_number, "key-press-event", (GCallback) _go_to_line_panel_on_goto_line_gtk_widget_key_press_event, self, 0);
	gtk_table_attach_defaults ((GtkTable*) self, (GtkWidget*) self->priv->line_number, (guint) 2, (guint) 3, (guint) 0, (guint) 1);
	_tmp2 = NULL;
	self->priv->line_count_lbl = (_tmp2 = g_object_ref_sink ((GtkLabel*) gtk_label_new ("")), (self->priv->line_count_lbl == NULL) ? NULL : (self->priv->line_count_lbl = (g_object_unref (self->priv->line_count_lbl), NULL)), _tmp2);
	gtk_label_set_use_markup (self->priv->line_count_lbl, TRUE);
	gtk_table_attach ((GtkTable*) self, (GtkWidget*) self->priv->line_count_lbl, (guint) 3, (guint) 4, (guint) 0, (guint) 1, GTK_SHRINK, GTK_SHRINK, (guint) 5, (guint) 0);
	g_signal_connect_object ((DocPanelBase*) self, "show-panel", (GCallback) _go_to_line_panel_on_show_panel_doc_panel_base_show_panel, self, 0);
	return self;
}


GoToLinePanel* go_to_line_panel_new (GtkSourceBuffer** source_buff, GtkSourceView** source_view) {
	return go_to_line_panel_construct (TYPE_GO_TO_LINE_PANEL, source_buff, source_view);
}


static gboolean go_to_line_panel_real_on_show_panel (GoToLinePanel* self) {
	char* _tmp2;
	char* _tmp1;
	char* _tmp0;
	g_return_val_if_fail (self != NULL, FALSE);
	gtk_text_view_set_editable ((GtkTextView*) ((DocPanelBase*) self)->source_view, FALSE);
	gtk_spin_button_set_range (self->priv->line_number, (double) 0, (double) gtk_text_buffer_get_line_count ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff));
	gtk_widget_grab_focus ((GtkWidget*) self->priv->line_number);
	_tmp2 = NULL;
	_tmp1 = NULL;
	_tmp0 = NULL;
	gtk_label_set_markup (self->priv->line_count_lbl, _tmp2 = g_strconcat (_tmp1 = g_strconcat (" z <b>", _tmp0 = g_strdup_printf ("%i", gtk_text_buffer_get_line_count ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff)), NULL), "</b>", NULL));
	_tmp2 = (g_free (_tmp2), NULL);
	_tmp1 = (g_free (_tmp1), NULL);
	_tmp0 = (g_free (_tmp0), NULL);
	return FALSE;
}


gboolean go_to_line_panel_on_show_panel (GoToLinePanel* self) {
	return GO_TO_LINE_PANEL_GET_CLASS (self)->on_show_panel (self);
}


gboolean go_to_line_panel_on_goto_line (GoToLinePanel* self, GtkSpinButton* sender, const GdkEventKey* event) {
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (sender != NULL, FALSE);
	gtk_spin_button_update (self->priv->line_number);
	if (_vala_strcmp0 (gdk_keyval_name ((*event).keyval), "Return") == 0) {
		GtkTextIter line_iter = {0};
		GtkTextMark* _tmp0;
		GtkTextMark* mark;
		gtk_text_buffer_get_iter_at_line ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, &line_iter, gtk_spin_button_get_value_as_int (self->priv->line_number));
		gtk_text_buffer_place_cursor ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, &line_iter);
		_tmp0 = NULL;
		mark = (_tmp0 = gtk_text_buffer_create_mark ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, "cursor", &line_iter, TRUE), (_tmp0 == NULL) ? NULL : g_object_ref (_tmp0));
		gtk_text_view_scroll_to_mark ((GtkTextView*) ((DocPanelBase*) self)->source_view, mark, 0.4, FALSE, (double) 0, (double) 0);
		gtk_text_buffer_delete_mark ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, mark);
		gtk_text_view_set_editable ((GtkTextView*) ((DocPanelBase*) self)->source_view, TRUE);
		gtk_widget_grab_focus ((GtkWidget*) ((DocPanelBase*) self)->source_view);
		doc_panel_base_close ((DocPanelBase*) self);
		(mark == NULL) ? NULL : (mark = (g_object_unref (mark), NULL));
	}
	return FALSE;
}


static void go_to_line_panel_class_init (GoToLinePanelClass * klass) {
	go_to_line_panel_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GoToLinePanelPrivate));
	G_OBJECT_CLASS (klass)->finalize = go_to_line_panel_finalize;
	GO_TO_LINE_PANEL_CLASS (klass)->on_show_panel = go_to_line_panel_real_on_show_panel;
}


static void go_to_line_panel_instance_init (GoToLinePanel * self) {
	self->priv = GO_TO_LINE_PANEL_GET_PRIVATE (self);
}


static void go_to_line_panel_finalize (GObject* obj) {
	GoToLinePanel * self;
	self = GO_TO_LINE_PANEL (obj);
	(self->priv->line_number == NULL) ? NULL : (self->priv->line_number = (g_object_unref (self->priv->line_number), NULL));
	(self->priv->line_count_lbl == NULL) ? NULL : (self->priv->line_count_lbl = (g_object_unref (self->priv->line_count_lbl), NULL));
	G_OBJECT_CLASS (go_to_line_panel_parent_class)->finalize (obj);
}


GType go_to_line_panel_get_type (void) {
	static GType go_to_line_panel_type_id = 0;
	if (go_to_line_panel_type_id == 0) {
		static const GTypeInfo g_define_type_info = { sizeof (GoToLinePanelClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) go_to_line_panel_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GoToLinePanel), 0, (GInstanceInitFunc) go_to_line_panel_instance_init, NULL };
		go_to_line_panel_type_id = g_type_register_static (TYPE_DOC_PANEL_BASE, "GoToLinePanel", &g_define_type_info, 0);
	}
	return go_to_line_panel_type_id;
}


static int _vala_strcmp0 (const char * str1, const char * str2) {
	if (str1 == NULL) {
		return -(str1 != str2);
	}
	if (str2 == NULL) {
		return str1 != str2;
	}
	return strcmp (str1, str2);
}




